package jtk.project4.fleet.screen.addCustomer;

import nl.coderight.jazz.action.Action;
import nl.coderight.jazz.form.FormLayout;
import nl.coderight.jazz.form.FormView;
import nl.coderight.jazz.form.control.GroupControl;
import nl.coderight.jazz.form.field.button.PushButton;
import nl.coderight.jazz.form.validation.ValidationError;


import java.awt.FlowLayout;
import java.util.List;

import jtk.project4.fleet.action.CancelAddNewCustomerAction;
import jtk.project4.fleet.action.SaveAddNewCustomer;
import jtk.project4.fleet.event.AddCustomerEvent;
import jtk.project4.fleet.field.AddCustomerField;

public class AddCustomerView extends FormView{
		//private TabControl filterTab;
		//private TabItem datafilteringTab;
		//private TabItem sortingTab;
		
		private PushButton saveButton;
		private PushButton cancelButton;
		//private PushButton previewButton;
		//private PushButton closeButton;
		private Action saveAction;
		private Action cancelAction;
		//private Action previewtAction;
		//private Action closeAction;
		
		public void handleEvent(AddCustomerEvent evt) {
			switch (evt.getType()) {
			case CANCEL:
				close();
				break;
				
			case SAVE:
				// check for validation errors
			
			List<ValidationError> errors = validate();
				if (!errors.isEmpty()) {
					// there are validation errors so do not submit...
					errors.get(0).getField().focus();
					break;
				} else {
					// submit form and close view
					submit();
					close();
					// fall-through!
				}
		
			default:
				// propagate event to controller
				propagateEvent(evt);
				break;
			}
		}
		
		@Override
		public void close() {
			super.close();
		}
		
		
		@Override
		protected void init() {
			// TODO Auto-generated method stub
			//createTabFields();		
			createAction();
			createFields();
			createLayout();
		}
		private void createAction(){
			cancelAction = new CancelAddNewCustomerAction();
			saveAction = new SaveAddNewCustomer();
			//addEmployeeAction = new AddEmployeeAction();
			//reportFilterAction = new ReportFilterAction();
		}
		
		private void createFields() {
			saveButton = new PushButton(saveAction);
			cancelButton = new PushButton(cancelAction);
			//previewButton = new PushButton("preview");
			//closeButton = new PushButton("close");
		}
		
		private void createLayout() {
			addField(createForm());
		}
		
		//private void createTabFields()
		//{
			//filterTab = new TabControl();
			//datafilteringTab = new TabItem("Data Filtering");
			//sortingTab = new TabItem("Sorting");
			//expirationTab = new TabItem("Expiration");
			//photoTab = new TabItem("Photo");
		//}
		
		//private FormField createTabs()
		//{
			//TabControl form = filterTab;
			//form.addTab(datafilteringTab, createDataFilteringTab());
			//form.addTab(sortingTab, createDataFilteringTab());
			//form.addTab(expirationTab, createGeneralTab());
			//form.addTab(photoTab, createGeneralTab());
			//return form;
		//}
		
		private GroupControl createNewVendor() {
			GroupControl form = new GroupControl();
			form.setLayout(new FormLayout())
			//.addField(new FilterEquipmentselectionField("EquipmentSelection"))
			//.addRow()
			//.addField(new FilterSpecifyAdditionalFilterCriteriaField("SpecifyAdditionalFilterCriteria"))
			//.addRow()
			//.addField(new DriverLicenseInformationField("driverlicenseinformation"))
			//.addRow()
			//.addField(new MicellaneousField("miscellaneous"))
			;				
			
			return form;
		}
		
		private GroupControl createForm()
		{
			GroupControl form = new GroupControl();
			
			form.addField(new AddCustomerField("customer"));
			form.addField(createButtons(), SOUTH);
			
			return form;
		}
		
		private GroupControl createGeneralForm() {
			GroupControl form = new GroupControl();
			//form.addField(createGeneralForm(), NORTH);
			form.addField(createButtons(), SOUTH);
			return form;
		}
		
		private GroupControl createButtons() {
			GroupControl buttons = new GroupControl();
			buttons.setLayout(new FlowLayout(FlowLayout.RIGHT));
			buttons.addField(saveButton);
			buttons.addField(cancelButton);
			//buttons.addField(previewButton);
			//buttons.addField(closeButton);
			return buttons;
		}
		
}

